/*
 *  Copyright (C) 2010 Glauco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components.managers;

import java.util.Properties;
import javax.swing.JTextField;
import org.balisunrise.application.Application;
import org.balisunrise.common.BindingGroup;
import org.balisunrise.components.Converter;
import org.balisunrise.components.ComponentConverterException;
import org.balisunrise.components.ComponentManager;
import org.balisunrise.swing.components.util.BaliComponentFocusListener;
import org.balisunrise.swing.components.util.BaliPlainDocument;

/**
 *
 * @author glacuo
 */
public class StringComponentManager implements ComponentManager<String, JTextField>{

    public Class<String> getType() {
        return String.class;
    }

    public Class<JTextField> getComponentType() {
        return JTextField.class;
    }

    public JTextField buildComponent(String componentName, Properties p) {

        int length = 60;

        if(p!= null)
            try{
                Object o = p.get("length");
                length = (Integer) o;
            } catch(Exception e){}

        JTextField aux = new JTextField(
                BaliPlainDocument.createUpperText(length), "", length);
        aux.addFocusListener(new BaliComponentFocusListener());

        aux.setName(componentName);
        aux.getAccessibleContext().setAccessibleName(componentName);
        
        return aux;
    }

    public void clearComponentValue(JTextField component) {
        if (null == component)
            return;
        if (component instanceof JTextField){
            component.setText("");
        }
    }

    public Object readValue(JTextField component) {
        
        if (null == component)
            return null;

        if (component instanceof JTextField){
            return ((JTextField) component).getText();
        }

        return null;
    }

    public String readConvertedValue(JTextField component,
            boolean emptyForAll) throws ComponentConverterException {

        Object obj = readValue(component);

        if (obj == null)
            return null;

        String value = (String)obj;

        if(value.length() == 0 && emptyForAll)
            return null;

        return value;

    }

    public void insertValue(String propertyValue, JTextField component) {
        
        if (component == null)
            return ;

        if (propertyValue == null){
            component.setText(new String());
            return ;
        }

        component.setText(propertyValue);
    }

    public void insertConvertedValue(Object propertyValue, JTextField component) {
        insertValue((String) propertyValue,component);
    }

    public Converter<String, ?> getConverter() {
        return null;
    }

    public void createBinding(BindingGroup bindingGroup, Object source,
            String property, JTextField component) {

        Application.get().getBindingBuilder().createBinding(
                bindingGroup,
                source,
                property,
                component,
                "text", // propriedade do componente a fazer o bind
                this,
                true,
                true);
    }
}
